//-------------------------------------------------------------------------------------
//automission
function runmissions()
	{
	if( energy>=missions[GM_getValue('selectMission', 1)][1])
		{
		if (location.href.indexOf(SCRIPT.name+'/jobs') != -1)
			{
			var mission = GM_getValue('selectMission', 1);
			var jobname = missions[mission][3];
			var currentTab = missions[mission][2];
			//var sform = document.getElementById(SCRIPT.appID+"doJob_"+missions[mission][4]);
			var sform = document.evaluate("//form[@id='"+SCRIPT.appID+"_doJob_"+missions[mission][4]+"']",document,null,9,null).singleNodeValue;
			if(GM_getValue('missionMastery', '') == "checked")
			//basic Mastery logic. 
				{
				var masteryList = document.evaluate("//td[@class='job_name']",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
				for (var i=0;i<missions.length;i++)
					{
					//build Mastery percentages and level array for current jobtier.
					if (missions[i][2]==currentTab)
						{
						itemIndex=missions[i][3];
						tempHTML=masteryList.snapshotItem(itemIndex).innerHTML;
						masteryLevel=parseInt(tempHTML.substring(tempHTML.indexOf('Level')+6,tempHTML.indexOf(' Master')));
						if (tempHTML.indexOf('Mastered')!=-1)
							{
							masteryPerc=100;
							} 
						else 
							{
							masteryPerc=parseInt(tempHTML.substring(tempHTML.indexOf('Level'),tempHTML.indexOf('%')).split(' ')[3]);
							}
						//addToLog(missions[i][0] + ' ' +missions[i][3] + ' ' + masteryPerc); //debug code
						curTabMastery[itemIndex]=new Array(masteryPerc,masteryLevel,i);
						}
					}
						
				if (curTabMastery[jobname][0]<100) 
					{
					//currently selected job is below 100%
					addToLog('Doing Job: '+missions[mission][0]+'. Current Mastery: Level '+curTabMastery[jobname][1]+' '+curTabMastery[jobname][0]+'%');
					sform.submit();
					flag = 1;
// return here, check if logic will fall through w/o
					return;
					}
				else
					{
					//iterate through jobs in current tier and select the one that isnt 100%
					if (jobname==(curTabMastery.length-1))
						{
						startJob=0;
						} 
					else
						{
						startJob=jobname+1;
						}
					Notcompleted=-1;
					for (var i=startJob;i<curTabMastery.length;i++)
						{
						if (curTabMastery[i][0]<100)
							{
							Notcompleted=curTabMastery[i][2];
							break;
							}
						}
					if (Notcompleted==-1)
						{
						//no non-completed jobs found. Move to next tier.
						addToLog('Tier '+currentTab+' mastered. Switching to next tier.');
						nextJobIndex=curTabMastery[curTabMastery.length-1][2]+1;
						GM_setValue('selectMission',nextJobIndex);
						window.location = "http://apps.facebook.com/"+SCRIPT.name+"/jobs.php?tab="+missions[nextJobIndex][2];
						flag = 1;
						}
					else
						{
						//non-completed job found.
						addToLog('Currently selected mission: '+missions[mission][0]+' already mastered. Moving to next mission.');
						GM_setValue('selectMission',Notcompleted);
						window.location = "http://apps.facebook.com/"+SCRIPT.name+"/jobs.php?tab="+missions[Notcompleted][2];
						flag = 1;
						}
					}
				} 
			else
				{
				//do the job if no tier mastery is set.
				sform.submit();
				flag = 1;
				}
			}
		else
			{
			window.location = "http://apps.facebook.com/"+SCRIPT.name+"/jobs.php?tab="+missions[GM_getValue('selectMission', 1)][2];
			flag = 1;
			return;
			}
		}
	}












